package it.enea.xlab.firstcrud.test;

import it.enea.xlab.firstcrud.crudmanager.CRUDManagerRemote;
import it.enea.xlab.firstcrud.entity.Supplier;
import it.enea.xlab.firstcrud.entity.TranslationType;
import it.enea.xlab.firstcrud.entity.TransmissionType;

import javax.naming.InitialContext;
import javax.naming.NamingException;


import org.junit.Test;

public class ClientTest2 {

	/**
	 * @author X-Lab
	 * @param args
	 */
	@Test
	public void main() {
	
		
		System.out.println("");
		System.out.println("------------test su jpa---------");
		System.out.println("");	
		
				try {
					System.out.println("0");	
					CRUDManagerRemote myBean1=createBeanJpa();
					test2(myBean1,"test2 on thread5SuJpa");
				} catch (NamingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}


	public CRUDManagerRemote createBeanJpa() throws NamingException
	{
		System.out.println("0-1");
		InitialContext ctx = new InitialContext();
		System.out.println("0-2");
		CRUDManagerRemote myBean = (CRUDManagerRemote) ctx.lookup("CRUDManagerImplJPAFacade/remote");
		System.out.println("0-3");
		return myBean;
	}
	

	
	public void test2(CRUDManagerRemote myBean, String thId)
	{
		System.out.println("------------------- test2 ----------:" + thId);
		System.out.println("1");
		Supplier mySupplier = new Supplier("xlab", new TransmissionType("http"), new TranslationType("eng"));
		System.out.println("2");
		Long id = myBean.createSupplier(mySupplier);
		System.out.println("3");
		System.out.println("CREATE - id: " + id);
		System.out.println("4");
		Supplier mySupplier2 = myBean.readSupplier(id);
		System.out.println("5");
		System.out.println("READ: identification value: " + mySupplier2.getIdentificationCode());
		mySupplier2.setIdentificationCode("Fabrizio");
		myBean.updateSupplier(mySupplier2);
		System.out.println("6");
		Supplier mySupplier3 = myBean.readSupplier(id);

		System.out.println("7");
		System.out.println("READ2: identification value: " + mySupplier3.getIdentificationCode());		
		
		
		//myBean.deleteSupplier(mySupplier3.getSupplierId());
		//mySupplier3 = myBean.readSupplier(id);

		//System.out.println("8");
		//System.out.println("READ3: identification value: " + mySupplier3.getIdentificationCode());		
				
		System.out.println("-------------------fine test2 ----------:" + thId);
	}
}
